package com.enniswu.weitter.base;

import android.os.Build;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.enniswu.weitter.R;
import com.enniswu.weitter.entity.RecreateEvent;
import com.enniswu.weitter.widget.AlertLoginDialog;
import com.enniswu.weitter.widget.LoadingDialog;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

import butterknife.ButterKnife;

public abstract class BaseActivity<P extends BaseContract.Presenter> extends AppCompatActivity implements BaseContract.View<P> {

    protected final LoadingDialog loadingDialog = new LoadingDialog();
    protected static Toast toast;
    protected P presenter;
    private TextView tvTitle;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
            //getWindow().setStatusBarColor(Color.WHITE);
        }
        EventBus.getDefault().register(this);
    }

    @Override
    public void setContentView(int layoutResID) {
        super.setContentView(layoutResID);
        tvTitle = findViewById(R.id.tv_title);
        ButterKnife.bind(this);
    }

    @Override
    protected void onDestroy() {
        if (presenter != null) {
            presenter.detachView();
            presenter = null;
        }
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    @Subscribe
    public void changeSetting(RecreateEvent event) {
        recreate();
    }

    @Override
    public void setTitle(CharSequence title) {
        if (tvTitle != null) {
            tvTitle.setText(title);
        }
    }

    @Override
    public void setTitle(int resId) {
        setTitle(getString(resId));
    }

    @Override
    public void setPresenter(P presenter) {
        this.presenter = presenter;
    }

    @Override
    public boolean isShowLoading() {
        return loadingDialog.isVisible();
    }

    @Override
    public void showMessage(final String msg) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                if (toast == null) {
                    toast = Toast.makeText(BaseActivity.this, msg, Toast.LENGTH_SHORT);
                } else {
                    toast.setText(msg);
                }
                toast.show();
            }
        });
    }

    @Override
    public void showMessage(final int resId) {
        showMessage(getString(resId));
    }

    @Override
    public void showLoading() {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                if (!isShowLoading() && !isDestroyed() && !isFinishing()) {
                    loadingDialog.show(getSupportFragmentManager(), "dialog_loading");
                }
            }
        });
    }

    @Override
    public void dismissLoading() {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                if (isShowLoading() && !isDestroyed() && !isFinishing())
                    loadingDialog.dismiss();
            }
        });
    }

    @Override
    public void showAlertLogin() {
        new AlertLoginDialog().show(getSupportFragmentManager(), "toStart");
    }
}
